package com.sls.game.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.NutsNetGoodsInfoDAO;
import com.sls.game.pojo.NutsNetGoodsInfo;
import com.sls.game.service.NutsNetGoodsAddService;

/**
 * @Description:坚果网络商品新增(或者更新)
 * @author Wangxuegang
 * @time 2012-08-28
 */
@Service("nutsNetGoodsAddService")
public class NutsNetGoodsAddServiceImpl implements NutsNetGoodsAddService {

	private static Logger log = Logger
			.getLogger(NutsNetGoodsAddServiceImpl.class);

	/**
	 * 坚果网络商品DAO
	 */
	private NutsNetGoodsInfoDAO nutsNetGoodsInfoDAO;

	private Gson gson = null;

	public NutsNetGoodsAddServiceImpl() {
		gson = CommonUtil.gson();
	}

	public NutsNetGoodsInfoDAO getNutsNetGoodsInfoDAO() {
		return nutsNetGoodsInfoDAO;
	}

	@Resource(name = "nutsNetGoodsInfoDAO")
	public void setNutsNetGoodsInfoDAO(NutsNetGoodsInfoDAO nutsNetGoodsInfoDAO) {
		this.nutsNetGoodsInfoDAO = nutsNetGoodsInfoDAO;
	}

	/**
	 * 坚果网络商品新增(或者更新)
	 */
	@Override
	public String insert(String body, Head head) throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}

		NutsNetGoodsInfo nutsNetGoodsInfo = gson.fromJson(body,
				NutsNetGoodsInfo.class);
		String partnerId = nutsNetGoodsInfo.getPartnerId();
		String supProductId = nutsNetGoodsInfo.getSupProductId();
		String unitPrice = nutsNetGoodsInfo.getUnitPrice();
		String name = nutsNetGoodsInfo.getName();
		String productType = nutsNetGoodsInfo.getProductType();
		String parvalue = nutsNetGoodsInfo.getParvalue();
		String ts = nutsNetGoodsInfo.getTs();
		String sign = nutsNetGoodsInfo.getSign();
		boolean flag = checkData(partnerId, supProductId, unitPrice, name,
				productType, parvalue, ts, sign, head.getRequestId());
		if (flag) { // 参数都不为空
			// 先查询数据表，看该商品是否存在，如果不存在做插入操作；如果存在，做更新操作
			NutsNetGoodsInfo nutsNetGoodsInfo1 = null;
			NutsNetGoodsInfo nutsNetGoods = new NutsNetGoodsInfo();
			nutsNetGoodsInfo1 = nutsNetGoodsInfoDAO.queryById(supProductId);

			if (nutsNetGoodsInfo1 == null) { // 商品新增
				nutsNetGoods.setId(UUID.randomUUID().toString()); // 随机字符串
				nutsNetGoods.setPartnerId(partnerId);
				nutsNetGoods.setSupProductId(supProductId);
				nutsNetGoods.setUnitPrice(unitPrice);
				nutsNetGoods.setName(name);
				nutsNetGoods.setProductType(productType);
				nutsNetGoods.setParvalue(parvalue);
				nutsNetGoods.setTs(ts);
				nutsNetGoods.setSign(sign);
				nutsNetGoods.setSerialNumber("");
				nutsNetGoods.setOpCode("");
				nutsNetGoods.setGoodsDelete("1"); // "0":该商品删除货源;"1":该商品正常供货
				nutsNetGoods.setIsAudit("0"); //0：未审核，1已审核)
				try {
					nutsNetGoodsInfoDAO.insert(nutsNetGoods);
					if (log.isInfoEnabled()) {
						log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
					}

					return null;
				} catch (Exception e) {
					log.info("Service处理[" + head.getRequestId()
							+ "]的请求出现数据插入异常！");
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}

			} else { // 商品更新（名称、类型、面值）
				nutsNetGoods.setId(nutsNetGoodsInfo1.getId());
				nutsNetGoods.setPartnerId(partnerId);
				nutsNetGoods.setSupProductId(supProductId);
				nutsNetGoods.setUnitPrice(unitPrice);
				nutsNetGoods.setName(name);
				nutsNetGoods.setProductType(productType);
				nutsNetGoods.setParvalue(parvalue);
				nutsNetGoods.setTs(ts);
				nutsNetGoods.setSign(sign);
				nutsNetGoods.setSerialNumber("");
				nutsNetGoods.setOpCode("");
				nutsNetGoods.setGoodsDelete("1"); // "0":该商品删除货源;"1":该商品正常供货
				nutsNetGoods.setIsAudit("0"); //0：未审核，1已审核)
				try {
					nutsNetGoodsInfoDAO.update(nutsNetGoods);
					if (log.isInfoEnabled()) {
						log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
					}

					return null;
				} catch (Exception e) {
					log.info("Service处理[" + head.getRequestId()
							+ "]的请求出现数据更新异常！");
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}
			}
		}
		return null;
	}

	private boolean checkData(String partnerId, String supProductId,
			String unitPrice, String name, String productType, String parvalue,
			String ts, String sign, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		// 合作平台编号
		if ("".equals(partnerId) || partnerId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_PLATFORMNO_NULL.getCode(),
					ExceptionCodeDes.COOPERATION_PLATFORMNO_NULL
							.getDescription());
		}
		// 商品中心商品编号
		else if ("".equals(supProductId) || supProductId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_ID_EMPTY.getCode(),
					ExceptionCodeDes.GOODS_ID_EMPTY.getDescription());
		}
		// 供货价格单价
		else if ("".equals(unitPrice) || unitPrice == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_SUPPLY_PRICE_NULL.getCode(),
					ExceptionCodeDes.GOODS_SUPPLY_PRICE_NULL.getDescription());
		}
		// 商品名称
		else if ("".equals(name) || name == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_NAME_NULL.getCode(),
					ExceptionCodeDes.GOODS_NAME_NULL.getDescription());
		}
		// 商品类型
		else if ("".equals(productType) || productType == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_TYPE_NULL.getCode(),
					ExceptionCodeDes.GOODS_TYPE_NULL.getDescription());
		}
		// 商品面值
		else if ("".equals(parvalue) || parvalue == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_PARVALUE_NULL.getCode(),
					ExceptionCodeDes.GOODS_PARVALUE_NULL.getDescription());
		}
		// 时间戳
		else if ("".equals(ts) || ts == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.TIME_STAMP_NULL.getCode(),
					ExceptionCodeDes.TIME_STAMP_NULL.getDescription());
		}
		// 签名字符串
		else if ("".equals(sign) || sign == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SIG_NATURE_NULL.getCode(),
					ExceptionCodeDes.SIG_NATURE_NULL.getDescription());
		} else {
			flag = true;
		}
		return flag;

	}

}
